namespace RivercrabNet
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Windows.Forms;

    public class PaintLineFibonacciTimeZones : PaintLineBase
    {
        public PaintLineFibonacciTimeZones()
        {
            base.LineType = PaintLineType.FibonacciTimeZones;
            base.PlName_zh = "斐波那契时间周期线";
        }

        public override void BeginMove()
        {
            if (base.PointList.Count != 0)
            {
                Point crossHairPoint = base.CgInstance.GetCrossHairPoint();
                ChartDiv local1 = base.CgInstance.DicChartDiv[base.OwnedPanelID];
                base.BeginMovePoint = crossHairPoint;
                base.MoveType = 0;
                base.CgInstance.Cursor = Cursors.Hand;
                base.BeginMoveValue.Clear();
                base.BeginMoveValue.Add(base.PointList[0]);
            }
        }

        public override void Draw(Graphics g, List<PaintLineBase.PointValue> pList, Color curColor)
        {
            if (pList.Count != 0)
            {
                ChartDiv div = base.CgInstance.DicChartDiv[base.OwnedPanelID];
                g.SmoothingMode = SmoothingMode.AntiAlias;
                Brush brush = new SolidBrush(curColor);
                Pen pen = new Pen(brush) {
                    Width = (float) base.LineWidth
                };
                if (base.LineStyle != null)
                {
                    pen.DashPattern = base.LineStyle;
                }
                double[] numArray = this.LineParam(pList);
                Font font = new Font("New Times Roman", 10f);
                StringFormat format = new StringFormat();
                for (int i = 0; i < numArray.Length; i++)
                {
                    int pos = (int) numArray[i];
                    if ((pos >= (base.CgInstance.FirstVisibleRecord - 1)) && (pos <= (base.CgInstance.LastVisibleRecord - 1)))
                    {
                        float num3 = base.CgInstance.GetX(pos) - base.CgInstance.LeftYScaleWidth;
                        g.DrawLine(pen, num3, 0f, num3, (float) base.CgInstance.GetWorkSpaceY(div.PanelID));
                        if ((i == 0) && base.Selected)
                        {
                            g.FillRectangle(brush, new RectangleF(num3 - (base.LineWidth * 2.5f), (((float) base.CgInstance.GetWorkSpaceY(div.PanelID)) / 2f) - (base.LineWidth * 2.5f), (float) (base.LineWidth * 5), (float) (base.LineWidth * 5)));
                        }
                        g.DrawString(Common.GetFibonacciValue(i).ToString(), font, brush, num3, 0f);
                    }
                }
                format.Dispose();
                brush.Dispose();
                pen.Dispose();
            }
        }

        public override bool Init(Point mp)
        {
            if (base.CgInstance.DataSource.Rows.Count > 0)
            {
                int indexByPoint = base.CgInstance.GetIndexByPoint(mp);
                double valueByPoint = base.CgInstance.GetValueByPoint(mp);
                string key = base.CgInstance.DataSource.Rows[indexByPoint][base.CgInstance.CalenderScaleField].ToString();
                base.PointList.Clear();
                base.PointList.Add(new PaintLineBase.PointValue(0, key, valueByPoint));
                return true;
            }
            return false;
        }

        public override bool JudgeSelect()
        {
            if (base.PointList.Count != 0)
            {
                Point crossHairPoint = base.CgInstance.GetCrossHairPoint();
                foreach (int num2 in this.LineParam())
                {
                    if ((num2 >= (base.CgInstance.FirstVisibleRecord - 1)) && (num2 <= (base.CgInstance.LastVisibleRecord - 1)))
                    {
                        float num3 = base.CgInstance.GetX(num2) - base.CgInstance.LeftYScaleWidth;
                        if ((crossHairPoint.X >= (num3 - (base.LineWidth * 2.5f))) && (crossHairPoint.X <= (num3 + (base.LineWidth * 2.5f))))
                        {
                            return true;
                        }
                    }
                }
            }
            return false;
        }

        public override double[] LineParam()
        {
            return this.LineParam(base.PointList);
        }

        public double[] LineParam(List<PaintLineBase.PointValue> pList)
        {
            if (pList.Count == 0)
            {
                return null;
            }
            PaintLineBase.PointValue value2 = pList[0];
            double num1 = value2.Value;
            PaintLineBase.PointValue value3 = pList[0];
            int num = base.CgInstance.GetRecordByTime(value3.Key) - 1;
            int index = 1;
            int fibonacciValue = 1;
            List<double> list = new List<double> {
                (double) num
            };
            while (((num + 1) + fibonacciValue) <= base.CgInstance.LastVisibleRecord)
            {
                fibonacciValue = Common.GetFibonacciValue(index);
                list.Add((double) (num + fibonacciValue));
                index++;
            }
            return list.ToArray();
        }

        public override void Move()
        {
            if (base.PointList.Count != 0)
            {
                Point crossHairPoint = base.CgInstance.GetCrossHairPoint();
                ChartDiv div = base.CgInstance.DicChartDiv[base.OwnedPanelID];
                if ((crossHairPoint.Y >= (div.Rect.Y + div.TitleHeight)) && (crossHairPoint.Y <= (div.Rect.Bottom - div.CalenderScale.Height)))
                {
                    int mouseOverIndex = base.CgInstance.GetMouseOverIndex();
                    base.CgInstance.GetMouseOverValue();
                    if (mouseOverIndex < 0)
                    {
                        mouseOverIndex = 0;
                    }
                    if (mouseOverIndex > (base.CgInstance.LastVisibleRecord - 1))
                    {
                        mouseOverIndex = base.CgInstance.LastVisibleRecord - 1;
                    }
                    if (base.MoveType == 0)
                    {
                        double num2 = crossHairPoint.Y - base.BeginMovePoint.Y;
                        double num3 = base.CgInstance.DicChartDiv[base.OwnedPanelID].MaxValue[0];
                        double num4 = base.CgInstance.DicChartDiv[base.OwnedPanelID].MinValue[0];
                        double num5 = (num2 / ((double) base.CgInstance.GetWorkSpaceY(base.OwnedPanelID))) * (num4 - num3);
                        int indexByPoint = base.CgInstance.GetIndexByPoint(base.BeginMovePoint);
                        int num7 = base.CgInstance.GetIndexByPoint(crossHairPoint);
                        PaintLineBase.PointValue value2 = base.BeginMoveValue[0];
                        int num8 = (base.CgInstance.GetRecordByTime(value2.Key) + (num7 - indexByPoint)) - 1;
                        if (num8 < 0)
                        {
                            num8 = 0;
                        }
                        else if (num8 > (base.CgInstance.DataSource.Rows.Count - 1))
                        {
                            num8 = base.CgInstance.DataSource.Rows.Count - 1;
                        }
                        PaintLineBase.PointValue value3 = base.BeginMoveValue[0];
                        base.PointList[0] = new PaintLineBase.PointValue(0, base.CgInstance.DataSource.Rows[num8][base.CgInstance.CalenderScaleField].ToString(), value3.Value + num5);
                    }
                }
            }
        }
    }
}

